package cn.gailvlun.gll.widget;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.animation.AnimationUtils;



public class RotatedDrawable extends Drawable implements Animatable, Runnable {

    private Bitmap mBitmap;
    private Paint mPaint;

    public RotatedDrawable() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);
    }

    public void setBitmap(Bitmap bitmap) {
        this.mBitmap = bitmap;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        if (mBitmap != null) {

            float loopPercent = calculateLoopPercent();
            canvas.translate(getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
            canvas.rotate(360 * loopPercent);
            canvas.drawBitmap(mBitmap, -getIntrinsicWidth() / 2, - getIntrinsicHeight() / 2, mPaint);
        }
    }

    
    private float calculateLoopPercent() {
        long howLong = AnimationUtils.currentAnimationTimeMillis() - startTime;
        float loopPercent = howLong / loopMills;
        if (loopPercent > 1) {
            startTime += loopMills;
        }
        return loopPercent;
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    private boolean mIsRunning;
    private long startTime;
    private float loopMills = 1 * 1000;

    @Override
    public void start() {
        if (!isRunning()) {
            mIsRunning = true;
            
            startTime = AnimationUtils.currentAnimationTimeMillis();
            run();
        }
    }

    @Override
    public void stop() {
        if (isRunning()) {
            unscheduleSelf(this);
            mIsRunning = false;
        }
    }

    @Override
    public boolean isRunning() {
        return mIsRunning;
    }

    @Override
    public void run() {
        invalidateSelf();
        scheduleSelf(this, AnimationUtils.currentAnimationTimeMillis() + (1000/60));
    }

    @Override
    public int getIntrinsicWidth() {
        if (mBitmap != null) {
            return mBitmap.getWidth();
        }
        return 0;
    }

    @Override
    public int getMinimumWidth() {
        if (mBitmap != null) {
            return mBitmap.getWidth();
        }
        return 0;
    }

    @Override
    public int getIntrinsicHeight() {
        if (mBitmap != null) {
            return mBitmap.getHeight();
        }
        return 0;
    }

    @Override
    public int getMinimumHeight() {
        if (mBitmap != null) {
            return mBitmap.getHeight();
        }
        return 0;
    }

    public void setLoopMills(long loopMills) {
        this.loopMills = loopMills;
    }
}
